
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_gip.h"

#include <sys/socket.h> 
#include <sys/ioctl.h> 
#include <netinet/in.h> 
#include <net/if.h> 

static int le_gip;

const zend_function_entry gip_functions[] = {
	PHP_FE(confirm_gip_compiled,	NULL)		
	PHP_FE(line_count,    NULL)           
	PHP_FE(get_server_ip , NULL )
	PHP_FE(get_size , NULL )	
	PHP_FE_END	
};

zend_module_entry gip_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"gip",
	gip_functions,
	PHP_MINIT(gip),
	PHP_MSHUTDOWN(gip),
	PHP_RINIT(gip),		
	PHP_RSHUTDOWN(gip),	
	PHP_MINFO(gip),
#if ZEND_MODULE_API_NO >= 20010901
	"0.1", 
#endif
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_GIP
ZEND_GET_MODULE(gip)
#endif

PHP_MINIT_FUNCTION(gip)
{
	return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(gip)
{
	return SUCCESS;
}

PHP_RINIT_FUNCTION(gip)
{
	return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(gip)
{
	return SUCCESS;
}

PHP_MINFO_FUNCTION(gip)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "gip support", "enabled");
	php_info_print_table_end();
}

PHP_FUNCTION(confirm_gip_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
		return;
	}

	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "gip", arg);
	RETURN_STRINGL(strg, len, 0);
}

PHP_FUNCTION( line_count )
{	
    char *filename = NULL;
    int name_len;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename , &name_len ) == FAILURE) {
        RETURN_NULL();
    }
    FILE *fp;
    char line_str[4096];
    int line = 0;
    fp = fopen( filename , "r");
    if(fp!=NULL)
    {   
	while(!feof(fp))
        {
	    if(fgets( line_str , sizeof(line_str), fp))
	    {
	        line++;
	    }
	}
        fclose(fp);
    }
    RETVAL_LONG( line );
    return;
}

PHP_FUNCTION( get_size )
{	
    char *filename = NULL;
    int name_len;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename , &name_len ) == FAILURE) {
        RETURN_NULL();
    }
	FILE *fp;
	int size=0;
	char c;
	if((fp=fopen( filename, "r")) == NULL ) {
		RETURN_NULL();
	}
	while((c=fgetc(fp))!=EOF){
		size++;
	}
	fclose(fp);
		
    RETVAL_LONG( size );
    return;
}

PHP_FUNCTION( get_server_ip )
{
	char *eth_name;
	int name_len;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &eth_name , &name_len ) == FAILURE) {
        RETURN_NULL();
    }
    int   sock; 
    struct   sockaddr_in   sin; 
    struct   ifreq   ifr; 
	char *ip_addr;
    sock   =   socket(AF_INET,   SOCK_DGRAM,   0); 
	if (sock   ==   -1) 
	{ 
		RETURN_NULL();
	} 

	strncpy(ifr.ifr_name,   eth_name,   IFNAMSIZ); 
	ifr.ifr_name[IFNAMSIZ   -   1]   =   0; 

	if (ioctl(sock,   SIOCGIFADDR,   &ifr)   <   0) 
	{ 
		RETURN_NULL();
	} 

	memcpy(&sin,   &ifr.ifr_addr,   sizeof(sin)); 
	int len = spprintf(&ip_addr, 0 , "%s", inet_ntoa(sin.sin_addr)); 
	RETURN_STRINGL(ip_addr, len, 0);	
	
}
